var currentPage = 1;
var gkeyword = "";
function doSearch(page) {
    document.getElementById("fmt-view").contentDocument.getElementById("progress").style.display = "block";
	var keyword = document.getElementById("searchfield").value;
	if (gkeyword != keyword) {
		gkeyword = keyword
		currentPage = 1;
	}
	keyword = Util.encodeQuery(keyword, "GB2312");
	
	var start = (currentPage - 1) * 10;
	if (page && page >=1) {
		start = (page -1) * 10;
	}
	var url = "http://www.google.com/custom?q=" + keyword + "&client=pub-5209090630180963&forid=1&ie=GB2312&oe=GB2312&safe=active&cof=GALT%3A%23008000%3BGL%3A1%3BDIV%3A%23336699%3BVLC%3A663399%3BAH%3Acenter%3BBGC%3AFFFFFF%3BLBGC%3A336699%3BALC%3A0000FF%3BLC%3A0000FF%3BT%3A000000%3BGFNT%3A0000FF%3BGIMP%3A0000FF%3BFORID%3A1&hl=zh-CN&start=" + start;
	var fxmlhttp = new XMLHttpRequest();
    fxmlhttp.open("GET",url,true);
    fxmlhttp.onreadystatechange=function() {
        if (fxmlhttp.readyState==4) {
            var rt= fxmlhttp.responseText;

            if (/\<br\>找不到和您的查询/.test(rt)) {
            	document.getElementById("fmt-view").contentDocument.getElementById("content").innerHTML = "No Result";
            	return;
            }
            var re = /div\sclass/;
    		var index = rt.indexOf("div id=res");
    		if (index > 0) {
    			rt = rt.substring(index-1);
    		}
    		rt = rt.replace(/href=\"\/custom/ig, "href=\"http://www.google.com/custom");
    		rt = rt.replace(/\<a\s/ig, '<a target="_blank" ');
    		rt = rt.replace(/margin-left/ig, "border-left: 1px dashed rgb(102, 102, 102); margin-top:-5px; margin-left");
    		mydocument = document.getElementById("fmt-view").contentDocument;
    		mydocument.getElementById("content").innerHTML = rt;
			var eleNav = mydocument.getElementById("res").childNodes[3];
    		mydocument.getElementById("content").innerHTML = mydocument.getElementById("res").childNodes[0].innerHTML;
    		mydocument.location.hash = '#__top';
    		mydocument.getElementById("injectEvent").click();
        }
    }
    document.getElementById("fmt-view").contentDocument.getElementById("progress").style.display = "none";
    try {
    	fxmlhttp.send(null);
    } catch(e){}
}

function doSearchNext() {
	currentPage++;
	doSearch(currentPage);
}
function doSearchPrev() {
	currentPage--;
	doSearch(currentPage);
}
function init() {
	document.getElementById("searchfield").select();	
}